#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

const int inf = INT_MAX / 2;
int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, K;
  cin >> n >> K;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i ++) cin >> a[i];
  vector<vector<int>> dp(n + 1, vector<int>(n + 1));
  vector<vector<vector<int>>> f(n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, inf)));
  for (int l = n; l >= 1; l --) {
    f[l][l][1] = 0;
    for (int r = l; r <= n; r ++) {
      dp[l][r] = INT_MAX;
      int len = r - l + 1;
      if (all_of(a.begin() + l, a.begin() + r + 1, [&](int x) { return x == a[l]; })) {
        dp[l][r] = max(0, K - len);
      }

      for (int i = l; i < r; i ++) {
        dp[l][r] = min(dp[l][r], dp[l][i] + dp[i + 1][r]);
      }

      if (a[l] == a[r]) {
        for (int k = l; k < r; k ++) {
          if (a[k] == a[l]) {
            for (int c = 2; c <= len; c ++) {
              f[l][r][c] = min(f[l][r][c], f[l][k][c - 1] + dp[k + 1][r - 1]);
            }
          }
        }
        for (int c = 1; c <= len; c ++) {
          dp[l][r] = min(dp[l][r], f[l][r][c] + max(0, K - c));
        }
      }
    }
  }
  cout << dp[1][n] << endl;
}